import axios from 'axios'

const api = axios.create({
  baseURL: 'http://localhost:3000/api',
  headers: {
    'Content-Type': 'application/json'
  }
})

// Add interceptor to include token with every request
api.interceptors.request.use(
  config => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers['x-auth-token'] = token
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

export default {
  // Auth API
  register(credentials) {
    return api.post('/auth/register', credentials)
  },
  login(credentials) {
    return api.post('/auth/login', credentials)
  },
  getUser() {
    return api.get('/auth/user')
  },
  
  // Tasks API
  getTasks() {
    return api.get('/tasks')
  },
  createTask(task) {
    return api.post('/tasks', task)
  },
  updateTask(id, task) {
    return api.put(`/tasks/${id}`, task)
  },
  deleteTask(id) {
    return api.delete(`/tasks/${id}`)
  }
} 